﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Reflection;
using System.IO;
using Zeny.Common.Utils;
using System.Collections;
using System.Xml;
using Zeny.UI.helper;

namespace Zeny.UI.startup
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private SuperTabItem getOpenedTab(String formCaption)
        {
            foreach(SuperTabItem tabItem in superTabControl1.Tabs)
            {
                if (formCaption.Equals(tabItem.Text))
                {
                    return tabItem;
                }
            }

            return null;
        }

        private Control createControl(String formClass)
        {
             Type type = Type.GetType(formClass);
             if (type != null)
             {
                 Control control = Activator.CreateInstance(type) as Control;
                 control.Dock = DockStyle.Fill;

                 return control;
             }
             else
             {
                 MessageBox.Show("配置错误，无法定位指定窗体！\r\n请尝试通过“系统管理->基础数据更新”功能进行更新。");
             }

             return null;
        }

        private void createTab(MenuItemEntity menuItem)
        {
            SuperTabItem tab = getOpenedTab(menuItem.formCaption);
            if(tab == null)
            {
                Control control = createControl(menuItem.formClass);

                if (control != null)
                {
                    tab = superTabControl1.CreateTab(menuItem.formCaption);
                    tab.AttachedControl.Controls.Add(control);
                }
            }

            if (tab != null)
            {
                superTabControl1.SelectedTab = tab;
            }
        }

        private void dotNetBarManager_ItemClick(object sender, EventArgs e)
        {
            BaseItem item = sender as BaseItem;
            if (item == null) return;

            if (item.Tag is MenuItemEntity)
            {
                MenuItemEntity menuItem = (MenuItemEntity)item.Tag;

                if (menuItem.isFormAction())
                {
                    createTab(menuItem);
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            MenuHelper.generateMenu(mainmenu);
        }
    }
}
